package com.ycq.httpclient.cookies;

import org.apache.http.HttpResponse;
import org.apache.http.client.CookieStore;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.cookie.Cookie;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.json.JSONException;
import org.json.JSONObject;
import org.testng.Assert;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

import java.io.IOException;
import java.util.List;
import java.util.Locale;
import java.util.ResourceBundle;

public class MyCookiesForPost {

    private String url;
    private ResourceBundle bundle;
    private CookieStore store;

    @BeforeTest
    public void beforTest() {
        bundle = ResourceBundle.getBundle("application", Locale.CHINA);
        url = bundle.getString("test.url");
    }

    @Test
    //获取cookies里面的信息
    public void testGetCookies() throws IOException {
        String result;
        //配置文件拼接url
        String uri = bundle.getString("getCookies.url");
        //注意拼接顺序
        String testUrl = this.url + uri;
        //访问
        HttpGet get = new HttpGet(testUrl);
        DefaultHttpClient client = new DefaultHttpClient();
        HttpResponse response = client.execute(get);
        result = EntityUtils.toString(response.getEntity(), "utf-8");
        System.out.println(result);

        this.store= client.getCookieStore();
        List<Cookie> storeCookies = store.getCookies();
        for (Cookie c : storeCookies) {
            String name = c.getName();
            String value = c.getValue();
            System.out.println("name=" + name + ";;;;value=" + value);
        }
    }
    @Test(dependsOnMethods ={ "testGetCookies"})
    private void testPostMethod() throws JSONException, IOException {
        String uri=bundle.getString("post.with.cookies");
        //拼接测试地址
        String testurl=this.url+uri;
        //声明一个Client对象，进行方法执行
        DefaultHttpClient client=new DefaultHttpClient();

        //声明一个方法 ，方法为Post方法
        HttpPost post=new HttpPost(testurl);
        //添加参数
        JSONObject param=new JSONObject();
        param.put("name","huhu");
        param.put("age","10");

        //设置请求头信息 设置header
        post.setHeader("content-type","application/json");
        //将参数信息添加到方法中
        StringEntity entity=new StringEntity(param.toString(),"utf-8");
        post.setEntity(entity);
        //声明一个对象进行响应结果储存
        String  result;
        //设置Cookies信息
        client.setCookieStore(this.store);
        //执行post方法
        HttpResponse response=client.execute(post);
        //获取响应结果
         result=EntityUtils.toString(response.getEntity(),"utf-8");
        System.out.println(result);
        //处理结果
        //返回的响应结果字符串转化为json对象
        JSONObject reultJson=new JSONObject(result);
        //获取结果值
        String success= (String) reultJson.get("huhu");
        String status= (String) reultJson.get("status");
        //判断返回结果值
        Assert.assertEquals("success",success);
        Assert.assertEquals("0",status);

    }


}
